package com.mdn.poi.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.mdn.poi.bean.Informe;
import com.mdn.poi.util.XlsHelper;

@SuppressWarnings("serial")
public class PoiXLSServlet extends HttpServlet {
	
	HSSFCellStyle dateStyle;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		// Literales para los textos de los títulos
		String[] titulos = {"Id", "País", "Ciudad", "Edificio", "Sala", "Inicio", "Fin", "Recursiva"};
		
		// Nombres de los campos del bean Informe en el orden en el que queremos que salgan en cada fila
		String[] campos = {"id", "pais", "ciudad", "edificio", "nombreSala", "inicioReserva", "finReserva", "recursiva"};
		
		// Datos de ejemplo
		List<Informe> lista = new ArrayList<Informe>();
		for(int i = 0;i<20;i++){
			lista.add(new Informe(Long.valueOf(i*1),"España", "Madrid", "Las Tablas II", "Sala I", new Date(), new Date(), false));
			lista.add(new Informe(Long.valueOf(i*2),"España", "Madrid", "Las Tablas II", "Sala II", new Date(), new Date(), false));
			lista.add(new Informe(Long.valueOf(i*3),"España", "Madrid", "Las Tablas II", "Sala III", new Date(), new Date(), false));
			lista.add(new Informe(Long.valueOf(i*4),"España", "Barcelona", "Edif. Barcelona", "Sala I", new Date(), new Date(), true));
			lista.add(new Informe(Long.valueOf(i*5),"España", "Barcelona", "Edif. Barcelona", "Sala II", new Date(), new Date(), false));
			lista.add(new Informe(Long.valueOf(i*6),"España", "Barcelona", "Edif. Barcelona", "Sala III", new Date(), new Date(), true));
		}
		HSSFWorkbook workbook = XlsHelper.getBook("Informe BBVA", titulos, campos, lista);

		// Devolvemos el libro con los datos
		resp.setHeader("Content-Disposition", "attachment; filename=informe.xls");
		resp.setContentType("application/vnd.ms-excel");		
		workbook.write(resp.getOutputStream());
	}
}
